﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Web;

namespace WorlducClient
{
    public partial class FrmGenXML : Form
    {
        public FrmGenXML()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            WriteXmlFile();

        }

        public void WriteXmlFile()
        {
            XmlDocument xmlDoc = new XmlDocument();
            //创建类型声明节点
            XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
            xmlDoc.AppendChild(node);
            //创建根节点
            XmlNode root = xmlDoc.CreateElement("questions");
            xmlDoc.AppendChild(root);

            foreach (var item in lbQAList.Items)
            {
                QANode qa = item as QANode;
               
                XmlElement opa = xmlDoc.CreateElement("option");
                opa.InnerText = "A. " + qa.optionA;

                XmlElement opb = xmlDoc.CreateElement("option");
                opb.InnerText = "B. " + qa.optionB;

                XmlElement opc = xmlDoc.CreateElement("option");
                opc.InnerText = "C. " + qa.optionC;

                XmlElement opd = xmlDoc.CreateElement("option");
                opd.InnerText = "D. " + qa.optionD;

                XmlElement ops = xmlDoc.CreateElement("options");
                ops.AppendChild(opa);
                ops.AppendChild(opb);
                ops.AppendChild(opc);
                ops.AppendChild(opd);

                XmlElement question = xmlDoc.CreateElement("question");
                question.InnerText = qa.question;

                XmlElement ans = xmlDoc.CreateElement("answer");
                ans.InnerText = qa.ans;


                XmlElement elqa = xmlDoc.CreateElement("qa");
                
                elqa.AppendChild(question);
                if(qa.type !="judge") elqa.AppendChild(ops);
                elqa.AppendChild(ans);

                elqa.SetAttribute("type",qa.type);

                root.AppendChild(elqa);

            }
            string path = AppDomain.CurrentDomain.BaseDirectory;
            path += "\\qa";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            try
            {
                string fileName = string.Format("{0:yyyyMMddHHmmss}.xml", DateTime.Now);
                xmlDoc.Save(path + "\\" + fileName);
                showMessage("保存成功!");
            }
            catch (Exception e)
            {
                //显示错误信息
                showMessage(e.Message);
            }
        }

        private string getQAType()
        {
            if (rdoSingle.Checked) return "single";
            if (rdoMulti.Checked) return "multi";
            return "judge";
        }
        private string getAns()
        {
            string ans = "";
            string type = getQAType();
            if ("judge" == type)
            {
                if (cbA.Checked) ans += "正确";
                if (cbB.Checked) ans += "错误";
            }
            else
            {
                if (cbA.Checked) ans += "A";
                if (cbB.Checked) ans += "B";
                if (cbC.Checked) ans += "C";
                if (cbD.Checked) ans += "D";
            }
            return ans;
        }

        private bool checkInput()
        {
            string type = getQAType();

            if (string.IsNullOrEmpty(rtQA.Text))
            {
                showMessage("请输入试题内容");
                return false ;
            }
            if (string.IsNullOrEmpty(rtA.Text))
            {

                showMessage("请输入选项A的内容");
                return false;
            }
            if (string.IsNullOrEmpty(rtB.Text))
            {

                showMessage("请输入选项B的内容");
                return false;
            }

            if (type != "judge" && string.IsNullOrEmpty(rtC.Text))
            {
                showMessage("请输入选项C的内容");
                return false;
            }
            if (type != "judge" && string.IsNullOrEmpty(rtD.Text))
            {
                showMessage("请输入选项D的内容");
                return false;
            }

            string ans = getAns();
            if (ans == "")
            {
                showMessage("请输入答案");
                return false;
            }
            if (type == "single" && ans.Length > 1)
            {
                showMessage("单选题只能有一个答案！");
                return false;
            }
            if (type == "judge" && ans.Length > 2)
            {
                showMessage("判断题只能有一个答案！");
                return false;
            }
            return true;
        }

        private void showMessage(string msg){
            MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {

            if (!checkInput()) return;
            QANode qa = new QANode
            {
                question = rtQA.Text.Trim(),
                optionA = rtA.Text.Trim(),
                optionB = rtB.Text.Trim(),
                optionC = rtC.Text.Trim(),
                optionD = rtD.Text.Trim(),
                ans = getAns(),
                type = getQAType()
            };
            lbQAList.Items.Add(qa);

        }

        private void rdoJudge_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoJudge.Checked)
            {
                rtA.Text = "正确";
                rtB.Text = "错误";
                rtC.Text = "";
                rtD.Text = "";
                cbA.Checked = false;
                cbB.Checked = false;
                cbC.Checked = false;
                cbD.Checked = false;
            }
        }

        private void rdoSingle_CheckedChanged(object sender, EventArgs e)
        {
            cbA.Checked = false;
            cbB.Checked = false;
            cbC.Checked = false;
            cbD.Checked = false;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            int index = lbQAList.SelectedIndex;
            if (index < 0)
            {
                showMessage("请选择一项进行编辑!");
                return;
            }
            if (!checkInput()) return;
            lbQAList.Items.RemoveAt(index);
            QANode qa = new QANode
            {
                question = rtQA.Text.Trim(),
                optionA = rtA.Text.Trim(),
                optionB = rtB.Text.Trim(),
                optionC = rtC.Text.Trim(),
                optionD = rtD.Text.Trim(),
                ans = getAns(),
                type = getQAType()
            };
            lbQAList.Items.Insert(index, qa);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (lbQAList.SelectedIndex >= 0)
            {
                lbQAList.Items.RemoveAt(lbQAList.SelectedIndex);
            }
        }

        private void lbQAList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbQAList.SelectedIndex >= 0)
            {
                QANode qa = lbQAList.SelectedItem as QANode;
                rtQA.Text = qa.question;
                rtA.Text = qa.optionA;
                rtB.Text = qa.optionB;
                rtC.Text = qa.optionC;
                rtD.Text = qa.optionD;
                cbA.Checked = false;
                cbB.Checked = false;
                cbC.Checked = false;
                cbD.Checked = false;
                string t = qa.type;
                if (t == "judge")
                {
                    rdoJudge.Checked = true;
                    if (qa.ans == "正确") cbA.Checked = true;
                    if (qa.ans == "错误") cbB.Checked = true;
                }
                else if (t == "single")
                {
                    rdoSingle.Checked = true;
                    if (qa.ans == "A") cbA.Checked = true;
                    if (qa.ans == "B") cbB.Checked = true;
                    if (qa.ans == "C") cbC.Checked = true;
                    if (qa.ans == "D") cbD.Checked = true;
                }
                else if (t == "multi")
                {
                    rdoMulti.Checked = true;
                    if (qa.ans.IndexOf("A") >= 0) cbA.Checked = true;
                    if (qa.ans.IndexOf("B") >= 0) cbB.Checked = true;
                    if (qa.ans.IndexOf("C") >= 0) cbC.Checked = true;
                    if (qa.ans.IndexOf("D") >= 0) cbD.Checked = true;
                }
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
            if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
            string fileName = dlg.FileName;
            XmlDocument xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.Load(fileName);
                XmlNodeList nodes = xmlDoc.SelectNodes("/questions/qa");
                foreach (XmlNode node in nodes)
                {
                    QANode qa = new QANode();
                    qa.type = node.Attributes["type"].InnerText;
                    qa.question = node.SelectSingleNode("question").InnerText;
                    qa.ans = node.SelectSingleNode("answer").InnerText;
                    if (qa.type != "judge")
                    {
                        XmlNodeList ops = node.SelectNodes("options/option");
                        string[] opstr = new string[4];
                        int i = 0;
                        foreach (XmlNode op in ops)
                        {
                            opstr[i++] = op.InnerText;
                        }
                        qa.optionA = QANode.filter(opstr[0], "A.");
                        qa.optionB = QANode.filter(opstr[1], "B.");
                        qa.optionC = QANode.filter(opstr[2], "C.");
                        qa.optionD = QANode.filter(opstr[3], "D.");
                        
                    }
                    else
                    {
                        qa.optionA = "正确";
                        qa.optionB = "错误";
                        qa.optionC = "";
                        qa.optionD = "";
                    }
                    lbQAList.Items.Add(qa);
                }
            }
            catch (Exception ex)
            {
                showMessage(ex.Message);
            }

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            lbQAList.Items.Clear();
        }
    }
    public class QANode
    {
        private string _question;
        private string _optionA;
        private string _optionB;
        private string _optionC;
        private string _optionD;
        public string question {
            get
            {
               return HttpUtility.HtmlDecode(_question);
            }
            set {
                _question = HttpUtility.HtmlEncode(value);
            }
        }
        public string type { get; set; }
        public string optionA
        {
            get
            {
                return HttpUtility.HtmlDecode(_optionA);
            }
            set
            {
                _optionA = HttpUtility.HtmlEncode(value);
            }
        }
        public string optionB
        {
            get
            {
                return HttpUtility.HtmlDecode(_optionB);
            }
            set
            {
                _optionB = HttpUtility.HtmlEncode(value);
            }
        }
        public string optionC
        {
            get
            {
                return HttpUtility.HtmlDecode(_optionC);
            }
            set
            {
                _optionC = HttpUtility.HtmlEncode(value);
            }
        }
        public string optionD
        {
            get
            {
                return HttpUtility.HtmlDecode(_optionD);
            }
            set
            {
                _optionD = HttpUtility.HtmlEncode(value);
            }
        }
        public string ans { get; set; }

        public override string ToString()
        {
            return question;
        }

        public static string filter(string str, string p){
            int i = str.IndexOf(p);
            if(i>=0){
                str = str.Substring(i+p.Length);
            }
            return str.Trim();
        }
    }
}
